package seunit.framework.automation;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
import java.io.*;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

@SuppressWarnings("unused")
/**
 * @class util
 * util.java
 * Purpose: Utilities for FrameWork
 *
 * @author Vishal Sangave
 * @version 1.0 5 June 2012
 */
public class util {
	private static File inputWorkbook;
	private static String testname;
	/**
	 * readList
	 * 
     * The readList method gets the execution List from Excel File.
     * Example : Iterable<String> listExecution=util.readList("..\\data\\ExecutionList.xls");
     * @author Vishal Sangave
     * @param String
     * @return Iterable<String>
     */
	public static Iterable<String> readList(String input) throws Exception, BiffException,FileNotFoundException,IOException{
		ArrayList<String> list = new ArrayList<String>();
		inputWorkbook = new File(input);
		FileInputStream fi = null;
		fi = new FileInputStream(inputWorkbook);
		Workbook w = null;
		w = Workbook.getWorkbook(fi);
		Sheet s=w.getSheet(0);
		for (int i = 1; i < s.getRows(); i++) {
			String execute = s.getCell(2,i).getContents();
			if(execute.equals("Yes")){
				testname=s.getCell(1,i).getContents();
				list.add(testname);
			}
		}
		fi.close();
		return list;
	}
	
	/**
	 * readList
	 * 
     * The readList method gets the execution List from Excel File.
     * Example : Iterable<String> listExecution=util.readList("..\\data\\ExecutionList.xls");
     * @author Vishal Sangave
     * @param String
     * @return Iterable<String>
     */
	public static void writeResultExcel(String input) throws Exception, BiffException,FileNotFoundException,IOException{
		
	}
	
	/**
	 * randEmail
	 * 
     * The randEmail method gets random string of length Specified
     * 
     * @author Vishal Sangave
     * @param Integer
     * @return String
     */
	public static String randEmail(Integer length){
		Random r = new Random();
		String newCode = "";
		while(length>0){
			char n = (char) (r.nextInt(26)+'a');
			newCode = newCode + n; 
			length--;
		}
		return newCode;
	}
	
	/**
	 * randEmail
	 * 
     * The randEmail method gets random string of length 4
     * 
     * @author Vishal Sangave
     * 
     * @return String
     */
	public static String randEmail(){
		Random r = new Random();
		Integer length = 4;
		String newCode = "";
		while(length>0){
			char n = (char) (r.nextInt(26)+'a');
			newCode = newCode + n; 
			length--;
		}
		return newCode;
	}
	
	/**
	 * randNumber
	 * 
     * The randNumber method gets random string of length specified
     * 
     * @author Vishal Sangave
     * @param String
     * @return String
     */
	public static String randNumber(Integer length){
		Random r = new Random();
		String newCode = "";
		while(length>0){
			char n = (char) (r.nextInt(10)+'0');
			newCode = newCode + n; 
			length--;
		}
		return newCode;
	}
	
	/**
	 * randNumber
	 * 
     * The randNumber method gets random string of length 4.
     * 
     * @author Vishal Sangave
     * 
     * @return String
     */
	public static String randNumber(){
		Random r = new Random();
		Integer length = 4;
		String newCode = "";
		while(length>0){
			char n = (char) (r.nextInt(10)+'0');
			newCode = newCode + n; 
			length--;
		}
		return newCode;
	}
}
